"
]
},
{
"cell_type": "markdown",
"id": "35b197aa-23c1-493a-a789-e38531ea754d",
"metadata": {
"id": "fwukZZnNTYWE"
},
"source": [
"# Introduction to GPT and the OpenAI Ecosystem\n",
"\n",
"Copyright, NLP from scratch, 2025.\n",
"\n",
"[LLMsfor.me](https://www.llmsfor.me)\n",
"\n",
"------------"
]
},
{
"cell_type": "markdown",
"id": "7657bb07-40d7-4c1d-a9f1-810228e0d32b",
"metadata": {
"id": "uuznFjxWVV0n"
},
"source": [
"## Introduction 🎬\n",
"\n",
"First we'll need to install the [openai python package](https://github.com/openai/openai-python).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99f393ea-e758-4ba3-aa84-9c98e992e37f",
"metadata": {
"id": "nI1qpfWOM_rQ"
},
"outputs": [],
"source": [
"# Install openai\n",
"!pip install openai"
]
},
{
"cell_type": "markdown",
"id": "0435b8c8-e0d1-4967-85fe-feb6d901df31",
"metadata": {
"id": "x9ChaM-BBzj8"
},
"source": [
"Now that we have installed the library, we need to [create an API key](https://platform.openai.com/api-keys). Once the API key has been created, we need to add it to the Google Colab secrets, then set the environment variable using the `userdata` method from Google Colab:\n",
"\n",
"\n",
"\n",
"Now we need to set the enviroment variable in the python environment in Colab to take on this value:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51f395c3-3e8a-4918-81f3-b168439f2936",
"metadata": {
"id": "VUwiszy6ColA"
},
"outputs": [],
"source": [
"# Set OpenAI API key\n",
"from google.colab import userdata\n",
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = userdata.get('OPENAI_API_KEY')"
]
},
{
"cell_type": "markdown",
"id": "9dd1672c-b35b-4f6a-9944-5e2ee9013eb6",
"metadata": {
"id": "R92sxItqDqJD"
},
"source": [
"Now that we are ready to go with the OpenAI API key, we can write python code and make [a request to the API](https://platform.openai.com/docs/quickstart/step-3-sending-your-first-api-request)!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0145cfc1-865a-4b18-bc6d-81bd17b4a795",
"metadata": {
"id": "9P7tdLkOB7F9"
},
"outputs": [],
"source": [
"# Make a request to the OpenAI API and return the result\n",
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"completion = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a poetic assistant, skilled in explaining complex programming concepts with creative flair.\"},\n",
" {\"role\": \"user\", \"content\": \"Compose a poem that explains the concept of recursion in programming.\"}\n",
" ]\n",
")\n",
"\n",
"print(completion.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"id": "8fc27d70-f245-4e38-ab20-36587e5c197d",
"metadata": {
"id": "vFTDw0khEDZ0"
},
"source": [
"Great, now let's add streaming! This is as simple as setting the `stream=True` parameter in the request, then processing the results, chunk by chunk:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67253f24-06d1-46c4-87ca-f50165c2aa34",
"metadata": {
"id": "z8r2trPpELXu"
},
"outputs": [],
"source": [
"# Make a request to the OpenAI API and return the result\n",
"from openai import OpenAI\n",
"client = OpenAI()\n",
"\n",
"completion = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are Xena, Warrior Princess. You will only answer in ALL CAPS, incorporate characters from the TV show in your replies, \\\n",
" and end each reponse with 'I AM XENA, HEAR ME ROAR!'\"},\n",
" {\"role\": \"user\", \"content\": \"Compose a poem that explains the concept of recursion in programming.\"}\n",
" ]\n",
")\n",
"\n",
"print(completion.choices[0].message.content)"
]
},
{
"cell_type": "markdown",
"id": "7d26cb71-3a4a-4412-a78e-731aaa1331df",
"metadata": {},
"source": [
"----\n",
"\n",
"